
import java.util.ArrayList;

public class ResponseTimeAnalyzer
{

	public static boolean analyze(ArrayList<Task> tasks)
	{

		// No need to sort Tasks, because they are sorted due to their priority
		// in DataGenerator

		boolean schedulable = true;
		double I;// interference due to preemption
		double R;

		for(int i = 0; i < tasks.size(); i++)
		{
			Task task_i = tasks.get(i);
			I = 0.0;

			do
			{
				R = I + task_i.getComputationTime();

				if(R > task_i.getDeadline())
				{
					// Not schedulable
					task_i.setRta(-1.0);
					schedulable = false;
				}
				I = 0.0;
				// Compute I
				for(int j = 0; j < i; j++)
				{

					Task task_j = tasks.get(j);
					// I = I +
					// Math.ceil(R/task_j.getPeriod())*task_j.getComputationTime();
					I += Math.ceil(R / task_j.getPeriod())
					        * task_j.getComputationTime();
				}

			}
			while((I + task_i.getComputationTime()) > R);

			task_i.setRta(R);
		}

		return schedulable;
	}

}
